#
# Makefile for the CORTEX_STM32F107_GCC_Eclipse FreeRTOS demo project.
#
# Created by Michael Scott.
#
# For use on the STM32-P107 Olimex board, uses the Codesourcery Lite EABI 
# toolchain for ARM. Before using on your own development environment, update 
# 'SOURCERY_INSTALL_DIR' to point to where you installed the toolchain. 
# 

#
# Base dir of the Codesourcery toolchain, modify this on new development setup!!
#
SOURCERY_INSTALL_DIR=/opt/CodeSourcery/Sourcery_G++_Lite

# 
# General configuration for project build
#
# GCC_VERSION 				-> Version of Codesourcery GCC to build with
# OUTPUT_NAME				-> File name for final build output, i.e. .elf file
# BUILD_DIR					-> Dir where output from build it put in
#
GCC_VERSION=4.5.2
OUTPUT_NAME=RTOSDemo
BUILD_DIR=build

#
# Project directories
#
# RTOS_SOURCE_DIR			-> RTOS source directory
# DEMO_COMMON_INCLUDE_DIR	-> Common demo header files
# DEMO_COMMON_SOURCE_DIR	-> Common demo source files
# DEMO_DRIVER_DIR			-> Common demo driver files
# UIP_COMMON_DIR			-> Networking files
# WEBSERVER_DEMO_DIR		-> WEB server demo files 
# SD_CARD_DEMO_DIR			-> SD card demo files 
# COM_PORT_DIR				-> COM port functionality files
# STM32F10X_SOURCE_DIR		-> Microcontroller specific drivers
# SOURCERY_GCC_INCLUDE_DIR	-> Sourcery library header files, specific to gcc
# SOURCERY_INCLUDE_DIR		-> Sourcery library header files
# SOURCERY_LINK_LIB_DIR		-> Sourcery thumb2 library files for linking
#
RTOS_SOURCE_DIR=../../Source
DEMO_COMMON_INCLUDE_DIR=../Common/include
DEMO_COMMON_SOURCE_DIR=../Common/Minimal
DEMO_DRIVER_DIR=../Common/drivers
UIP_COMMON_DIR=../Common/ethernet/uIP/uip-1.0/uip
WEBSERVER_DEMO_DIR=webserver
SD_CARD_DEMO_DIR=SD
COM_PORT_DIR=comport
STM32F10X_SOURCE_DIR=../Common/drivers/ST/STM32F10xFWLib/src
SOURCERY_GCC_INCLUDE_DIR=$(SOURCERY_INSTALL_DIR)/lib/gcc/arm-none-eabi/$(GCC_VERSION)/include
SOURCERY_INCLUDE_DIR=$(SOURCERY_INSTALL_DIR)/arm-none-eabi/include
SOURCERY_LINK_LIB_DIR=$(SOURCERY_INSTALL_DIR)/arm-none-eabi/lib/thumb2

#
# Cross compilation tools (CodeSourcery)
#
CC=arm-none-eabi-gcc
LD=arm-none-eabi-ld
AS=arm-none-eabi-as
OBJCOPY=arm-none-eabi-objcopy

#
# Flags to use for linker
# 
# - Use traditional format for output file
# - All warnings are errors
# - Delete all temporary local symbols
# - Add all symbols to the dynamic symbol table
# - Set the text and data sections to be readable and writable, also do not 
# page-align the data segment
# - Do not use the standard system startup files when linking
# - Use LINKER_SCRIPT as the linker script
# - Output map of linkage to build dir  
# - Output linkage to build dir
# 
LINKER_FLAGS=--traditional-format \
		--fatal-warnings \
		--discard-locals \
		--export-dynamic \
		--omagic \
		-nostartfiles \
		--script=$(LINKER_SCRIPT) \
		-Map $(BUILD_DIR)/$(OUTPUT_NAME).map \
		-o $(BUILD_DIR)/$(OUTPUT_NAME).elf
		
#
# Flags to use for compiler
# 
# - All compiler warnings, full debugging output for gdb, no optimisation, c99 
# language standard 
# - Build for the Cortex-M3
# - No line wrapping for error messages
# - Disable strict aliasing optimisation
#
# NOTE, including -Os flag messes up the COM port output, as does -O0/1!!
#
CFLAGS=-Wall -Wextra -ggdb -O2 -std=c99 \
		-mcpu=cortex-m3 -march=armv7-m -mfix-cortex-m3-ldrd -mthumb -mlittle-endian -mfpu=vfp -mfloat-abi=soft \
		-fmessage-length=0 \
		-fno-strict-aliasing \
		-nostdinc \
		-fno-common \
		-fno-builtin \
		-ffunction-sections \
		-fdata-sections \
		-fno-dwarf2-cfi-asm \
		-I . \
		-I $(SOURCERY_GCC_INCLUDE_DIR) \
		-I $(SOURCERY_INCLUDE_DIR) \
		-I $(RTOS_SOURCE_DIR)/include \
		-I $(RTOS_SOURCE_DIR)/portable/GCC/ARM_CM3 \
		-I $(DEMO_COMMON_INCLUDE_DIR) \
		-I $(DEMO_DRIVER_DIR)/ST/STM32F10xFWLib/inc \
		-I $(UIP_COMMON_DIR) \
		-I $(WEBSERVER_DEMO_DIR) \
		-I $(SD_CARD_DEMO_DIR) \
		-I $(COM_PORT_DIR) \
		-D PACK_STRUCT_END=__attribute\(\(packed\)\) \
		-D ALIGN_STRUCT_END=__attribute\(\(aligned\(4\)\)\) \
		-D __FLASH_BUILD \
		-D STM32F10X_CL -D GCC_ARMCM3 \
		-D _FLASH \
		-D DEBUG \
		-D STARTUP_FROM_RESET 
					
#
# Demo miscellaneous files 
#
CRT0_FILE=thumb_crt0.s
STARTUP_FILE=STM32F10x_Startup.s
LINKER_SCRIPT=RTOSDemo.ld

#
# Codesourcery library object files to link in
#
LINK_LIBS=$(SOURCERY_LINK_LIB_DIR)/libc.a
		
# 
# Source files to compile to build RTOSDemo.elf
#		
THUMB_SOURCE=$(DEMO_COMMON_SOURCE_DIR)/flash.c \
		$(DEMO_COMMON_SOURCE_DIR)/recmutex.c \
		$(DEMO_COMMON_SOURCE_DIR)/semtest.c \
		$(DEMO_COMMON_SOURCE_DIR)/BlockQ.c \
		$(DEMO_COMMON_SOURCE_DIR)/GenQTest.c \
		$(DEMO_COMMON_SOURCE_DIR)/integer.c \
		$(DEMO_COMMON_SOURCE_DIR)/PollQ.c \
		$(DEMO_COMMON_SOURCE_DIR)/QPeek.c \
		$(RTOS_SOURCE_DIR)/tasks.c \
		$(RTOS_SOURCE_DIR)/list.c \
		$(RTOS_SOURCE_DIR)/queue.c \
		$(RTOS_SOURCE_DIR)/portable/MemMang/heap_2.c \
		$(RTOS_SOURCE_DIR)/portable/GCC/ARM_CM3/port.c \
		main.c \
		./ParTest/ParTest.c \
		$(STM32F10X_SOURCE_DIR)/stm32f10x_usart.c \
		$(STM32F10X_SOURCE_DIR)/stm32f10x_gpio.c \
		$(STM32F10X_SOURCE_DIR)/stm32f10x_lib.c \
		$(STM32F10X_SOURCE_DIR)/stm32f10x_nvic.c \
		$(STM32F10X_SOURCE_DIR)/stm32f10x_rcc.c \
		$(STM32F10X_SOURCE_DIR)/stm32f10x_systick.c \
		$(COM_PORT_DIR)/com_port.c \
		printf-stdarg.c \
		$(STM32F10X_SOURCE_DIR)/stm32f10x_tim.c \
		$(STM32F10X_SOURCE_DIR)/stm32f10x_spi.c \
		$(STM32F10X_SOURCE_DIR)/stm32f10x_it.c \
		$(STM32F10X_SOURCE_DIR)/stm32f10x_exti.c \
		$(STM32F10X_SOURCE_DIR)/stm32f10x_dma.c \
		$(STM32F10X_SOURCE_DIR)/stm32fxxx_eth.c \
		timertest.c \
		$(SD_CARD_DEMO_DIR)/mmc.c \
		$(SD_CARD_DEMO_DIR)/sd_ll_spi.c \
		$(SD_CARD_DEMO_DIR)/sd_spi_mode.c \
		$(UIP_COMMON_DIR)/timer.c \
		$(UIP_COMMON_DIR)/uip.c \
		$(UIP_COMMON_DIR)/uip_arp.c \
		$(UIP_COMMON_DIR)/psock.c \
		$(WEBSERVER_DEMO_DIR)/uIP_Task.c \
		$(WEBSERVER_DEMO_DIR)/httpd.c \
		$(WEBSERVER_DEMO_DIR)/httpd-cgi.c \
		$(WEBSERVER_DEMO_DIR)/httpd-fs.c \
		$(WEBSERVER_DEMO_DIR)/http-strings.c \
		$(WEBSERVER_DEMO_DIR)/emac.c
		
ASSEMBLER_SOURCE=$(STARTUP_FILE) \
		$(CRT0_FILE)
						
#
# Rules for running make
#  
THUMB_OBJS = $(THUMB_SOURCE:.c=.o)

ASSEMBLER_OBJS = $(ASSEMBLER_SOURCE:.s=.o)

all: envsetup $(OUTPUT_NAME).bin

$(OUTPUT_NAME).bin : $(OUTPUT_NAME).hex
	$(OBJCOPY) $(BUILD_DIR)/$(OUTPUT_NAME).elf -O binary $(BUILD_DIR)/$(OUTPUT_NAME).bin
	 
$(OUTPUT_NAME).hex : $(OUTPUT_NAME).elf
	$(OBJCOPY) $(BUILD_DIR)/$(OUTPUT_NAME).elf -O ihex $(BUILD_DIR)/$(OUTPUT_NAME).hex

$(OUTPUT_NAME).elf : $(THUMB_OBJS) $(ASSEMBLER_OBJS) Makefile
	$(LD) $(LINKER_FLAGS) --start-group $(BUILD_DIR)/*.o $(LINK_LIBS) --end-group
	
$(THUMB_OBJS) : %.o : %.c Makefile FreeRTOSConfig.h
	$(CC) $(CFLAGS) -c $< -o $(BUILD_DIR)/$(basename $(notdir $@)).o

$(ASSEMBLER_OBJS) : %.o : %.s Makefile FreeRTOSConfig.h
	$(CC) $(CFLAGS) -c $< -o $(BUILD_DIR)/$(basename $(notdir $@)).o
	
envsetup : 
	mkdir -p $(BUILD_DIR)	
	
clean:
	rm -rf $(BUILD_DIR)
